package com.inplaceedit.client.activity.main.contact;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DateLabel;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.inplaceedit.client.activity.main.contact.ContactDetailsActivityImpl.ObjectBirthdayEditor;
import com.inplaceedit.client.activity.main.contact.ContactDetailsActivityImpl.ObjectNameEditor;
import com.inplaceedit.client.activity.main.contact.ContactDetailsActivityImpl.ObjectSexEditor;
import com.inplaceedit.client.ui.InPlaceDialogPanel;
import com.inplaceedit.client.ui.InPlaceEditPanel;
import com.inplaceedit.client.ui.SaveCancelDialog;
import com.inplaceedit.client.ui.contact.ContactNameDisplay;
import com.inplaceedit.client.ui.contact.ContactNameDisplayView;
import com.inplaceedit.client.ui.contact.ContactNameEditor;
import com.inplaceedit.client.ui.contact.ContactNameEditorView;
import com.inplaceedit.client.ui.contact.ContactSexDisplayView;
import com.inplaceedit.client.ui.contact.ContactSexSelectorView;
import com.inplaceedit.client.ui.note.NoteEditor;
import com.inplaceedit.client.ui.note.NoteEditorView;
import com.inplaceedit.client.ui.note.NoteListDisplay;
import com.inplaceedit.client.ui.note.NoteListDisplayView;
import com.inplaceedit.client.ui.phone.PhoneEditor;
import com.inplaceedit.client.ui.phone.PhoneEditorView;
import com.inplaceedit.client.ui.phone.PhoneListDisplay;
import com.inplaceedit.client.ui.phone.PhoneListDisplayView;
import com.inplaceedit.shared.dto.ContactDto;

public class ContactDetailsView extends Composite implements
		ContactDetailsActivityImpl.View {

	interface Binder extends UiBinder<Widget, ContactDetailsView> {
	}

	private static Binder uiBinder = GWT.create(Binder.class);

	@UiField
	DeckPanel mainPanel;
	@UiField
	InPlaceDialogPanel newNotePanel;
	@UiField
	Anchor newNoteCommand;
	@UiField
	NoteEditorView newNoteEditor;
	@UiField
	NoteListDisplayView noteListDisplay;
	@UiField
	InPlaceEditPanel namePanel;
	@UiField
	ContactNameDisplayView nameDisplay;
	@UiField
	ContactNameEditorView nameEditor;
	@UiField
	InPlaceEditPanel birthdayPanel;
	@UiField
	DateLabel birthdayDisplay;
	@UiField
	DateBox birthdayEditor;
	@UiField
	InPlaceEditPanel sexPanel;
	@UiField
	ContactSexDisplayView sexDisplay;
	@UiField
	ContactSexSelectorView sexSelector;
	@UiField
	PhoneListDisplayView phoneListDisplay;
	@UiField
	InPlaceDialogPanel newPhonePanel;
	@UiField
	Anchor newPhoneCommand;
	@UiField
	PhoneEditorView newPhoneEditor;

	private Handler handler;

	private SaveCancelDialog nameDialog;
	private SaveCancelDialog birthdayDialog;
	private SaveCancelDialog sexDialog;
	private SaveCancelDialog newNoteDialog;

	private final ObjectNameEditor objectNameEditor = new ObjectNameEditor() {
		@Override
		public ContactNameEditor getEditor() {
			return nameEditor;
		}
	};
	private final ObjectBirthdayEditor objectBirthdayEditor = new ObjectBirthdayEditor() {
		@Override
		public LeafValueEditor<Date> getEditor() {
			return birthdayEditor.asEditor();
		}
	};

	private final ObjectSexEditor objectSexEditor = new ObjectSexEditor() {
		@Override
		public LeafValueEditor<ContactDto.Sex> getEditor() {
			return sexSelector.asEditor();
		}
	};

	public ContactDetailsView() {
		initWidget(uiBinder.createAndBindUi(this));

		birthdayEditor.setFormat(new DateBox.DefaultFormat(DateTimeFormat
				.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT)));

		namePanel.getEditCommand().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				if (handler != null) {
					handler.onEditName();
				}
			}
		});
		birthdayPanel.getEditCommand().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				if (handler != null) {
					handler.onEditBirthday();
				}
			}
		});
		sexPanel.getEditCommand().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				if (handler != null) {
					handler.onEditSex();
				}
			}
		});
	}

	@Override
	public SaveCancelDialog getBirthdayDialog() {
		if (birthdayDialog == null) {
			birthdayDialog = birthdayPanel.asDialog();
		}
		return birthdayDialog;
	}

	@Override
	public LeafValueEditor<Date> getBirthdayDisplay() {
		return birthdayDisplay.asEditor();
	}

	@Override
	public ObjectBirthdayEditor getBirthdayEditor() {
		return objectBirthdayEditor;
	}

	@Override
	public SaveCancelDialog getNameDialog() {
		if (nameDialog == null) {
			nameDialog = namePanel.asDialog();
		}
		return nameDialog;
	}

	@Override
	public ContactNameDisplay getNameDisplay() {
		return nameDisplay;
	}

	@Override
	public ObjectNameEditor getNameEditor() {
		return objectNameEditor;
	}

	@Override
	public SaveCancelDialog getNewNoteDialog() {
		if (newNoteDialog == null) {
			newNoteDialog = newNotePanel.asDialog();
		}
		return newNoteDialog;
	}

	@Override
	public NoteEditor getNewNoteEditor() {
		return newNoteEditor;
	}

	@Override
	public SaveCancelDialog getNewPhoneDialog() {
		return newPhonePanel.asDialog();
	}

	@Override
	public PhoneEditor getNewPhoneEditor() {
		return newPhoneEditor;
	}

	@Override
	public NoteListDisplay getNoteListDisplay() {
		return noteListDisplay;
	}

	@Override
	public PhoneListDisplay getPhoneListDisplay() {
		return phoneListDisplay;
	}

	@Override
	public SaveCancelDialog getSexDialog() {
		if (sexDialog == null) {
			sexDialog = sexPanel.asDialog();
		}
		return sexDialog;
	}

	@Override
	public LeafValueEditor<ContactDto.Sex> getSexDisplay() {
		return sexDisplay.asEditor();
	}

	@Override
	public ObjectSexEditor getSexEditor() {
		return objectSexEditor;
	}

	@Override
	public void hideDialogs() {
		newNotePanel.hideDialog();
		namePanel.hideEditor();
		birthdayPanel.hideEditor();
	}

	@Override
	public void setHandler(final Handler handler) {
		this.handler = handler;
	}

	@Override
	public void showDetails(final boolean show) {
		mainPanel.showWidget(show ? 0 : 1);
	}

	@UiHandler("newNoteCommand")
	void onNewNoteCommandClick(final ClickEvent event) {
		if (handler != null) {
			handler.onNewNote();
		}
	}

	@UiHandler("newPhoneCommand")
	void onNewPhoneCommandClick(final ClickEvent event) {
		if (handler != null) {
			handler.onNewPhone();
		}
	}
}
